const express = require('express');
//导入session中间件
const session = require('express-session');
const app = express();
app.use(express.static('./view'))
app.use(express.urlencoded({ extended: false }));

//2.配置session中间件
app.use(session({
    secret: 'keybody',
    resave: false,
    saveUninitialized: true
}))
app.post('/api/login', (req, res) => {
    console.log(req.body.user);
    if (req.body.user != 'admin' || req.body.age != '123') {
        return res.send({ status: 1, msg: '登入失败' })
    }
    req.session.user = req.body;//将用户的信息存储到session
    req.session.islogin = true;//将用户的登入状态存储到session中
    res.send({ status: 0, username: req.session.user.user })
})
app.get('/api/index', (req, res) => {
    //判断用户是否登入
    if (!req.session.islogin) {

        return res.send({
            status: 1,
            msg: '用户没有登入'
        })

    }
    res.send({
        status: 0,
        msg: '登入成功!',
        resu: req.session.user,
    })
})
//退出登录
app.post('/api/logout', (req, res) => {
    //req.seesion.destroy();//清空session保存的信息
    req.session.destroy();
    res.send({
        status: 0,
        msg: '退出登录成功'
    })
})
app.listen(88, () => {
    console.log('http://127.0.0.1:88');
})